package com.zhou.base.components.bs.common.model;


import lombok.Data;
import lombok.NoArgsConstructor;

import static com.zhou.base.components.bs.common.enums.ResponseEnum.FAIL;
import static com.zhou.base.components.bs.common.enums.ResponseEnum.SUCCESS;

/**
 * 注意feign 返回的对象如果是 当前类的话，可能会示例化错误，没有提供set
 *
 * @author xianfeng
 * @date 2023/7/25 16:42
 */
@Data
@NoArgsConstructor
public class Response<T> {

    private Integer code;
    private String msg;
    private T data;

    public Boolean isSuccess() {
        if (SUCCESS.getCode().equals(code)) {
            return true;
        }
        return false;
    }

    public static Response success() {
        return restResult(null, SUCCESS.getCode(), SUCCESS.getMsg());
    }

    public static <T> Response<T> success(T data) {
        return restResult(data, SUCCESS.getCode(), SUCCESS.getMsg());
    }

    public static <T> Response<T> successMsg(String msg) {
        return restResult(null, SUCCESS.getCode(), msg);
    }

    public static <T> Response<T> success(String msg, T data) {
        return restResult(data, SUCCESS.getCode(), msg);
    }

    public static <T> Response<T> fail() {
        return restResult(null, FAIL.getCode(), FAIL.getMsg());
    }

    public static <T> Response<T> failMsg(String msg) {
        return restResult(null, FAIL.getCode(), msg);
    }

    public static <T> Response<T> fail(T data) {
        return restResult(data, FAIL.getCode(), FAIL.getMsg());
    }

    public static <T> Response<T> fail(String msg, T data) {
        return restResult(data, FAIL.getCode(), msg);
    }

    public static <T> Response<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> Response<T> fail(int code, String msg, T data) {
        return restResult(data, code, msg);
    }

    private static <T> Response<T> restResult(T data, int code, String msg) {
        Response<T> r = new Response<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }

    public static void main(String[] args) {

    }
}
